package com.jk1123.igs.common.vo;

import lombok.Data;
import org.slf4j.MDC;

import java.io.Serializable;

/**
 * @author huyoufu <https://github.com/huyoufu>
 * @TIME 2024/1/3 13:56
 * @description
 */
@Data
public class R <T> implements Serializable {
    private int code;
    private String message;
    private T data;
    /**
     * 请求跟踪的id 用于排查问题定位
     */
    private String traceId;

    //私有化构造
    private R() {
    }
    //私有化构造
    private R(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
    public static <T>R<T> of(RCode rCode, T data) {
        return new R<>(rCode.getCode(), rCode.getMessage(), data);
    }
    public static <T>R<T> of(RCode rCode, String message, T data) {
        return new R<>(rCode.getCode(), message, data);
    }
    public static <T>R<T> successOf(String message,T data) {
        return new R<>(RCode.SUCCESS.getCode(), message, data);
    }
    public static <T>R<T> successOf(T data) {
        return new R<>(RCode.SUCCESS.getCode(), RCode.SUCCESS.getMessage(), data);
    }
    public static <T>R<T> failOf(String message) {
        return new R<>(RCode.FAIL.getCode(), message, null);
    }
    public static <T>R<T> failOf(String message,T data) {
        return new R<>(RCode.FAIL.getCode(), message, data);
    }
    public static <T>R<T> failOf() {
        return new R<>(RCode.FAIL.getCode(), RCode.FAIL.getMessage(), null);
    }
    public static <T>R<T> errorOf() {
        return new R<>(RCode.SYSTEM_ERROR.getCode(), RCode.SYSTEM_ERROR.getMessage(), null);
    }
    public static <T>R<T> errorOf(String message) {
        return new R<>(RCode.SYSTEM_ERROR.getCode(), message, null);
    }
    public static <T>R<T> errorOf(String message,T data) {
        return new R<>(RCode.SYSTEM_ERROR.getCode(), message, data);
    }


}
